/**
 * 这个文件用于封装axios
 */
import axios from 'axios';
import {BASE_URL,TIMEOUT,CONTENT_TYPE} from './config';
axios.defaults.baseURL = BASE_URL;
axios.defaults.timeout = TIMEOUT; 
axios.defaults.headers.common['Content-Type'] = CONTENT_TYPE;

//响应拦截器
axios.interceptors.response.use(
    response => {
        //如果reponse里面的status是200，说明访问到接口了，否则错误
        if(response.status == 200){
            return Promise.resolve(response);
        }else{
            return Promise.reject(response);
        }
    },
    error => {
        if(error.response.status){
            return Promise.reject(error.response);
        }
    }
);

/**
 * 封装get方法
 */
export function get(url,params={}){
    return new Promise((resolve,reject) => {
        axios.get(url,{params:params})
        .then(response =>{
            resolve(response.data);
        })
        .catch(err =>{
            reject(err);
        })
    });
}

/**
 * 封装post方法
 */
export function post(url,data={},config){
    return new Promise((resolve,reject) => {
        axios.post(url,data,config)
        .then(response =>{
            resolve(response.data);
        })
        .catch(err =>{
            reject(err);
        })
    });
}